package com.ideaaedi.plugin.fastcoding.settings;

import com.ideaaedi.plugin.fastcoding.constant.FastCodingConstant;
import com.ideaaedi.plugin.fastcoding.ui.FastCodingSettings;
import com.ideaaedi.plugin.fastcoding.util.PropertiesComponentUtil;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.project.Project;
import com.intellij.util.xmlb.XmlSerializerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;


/**
 * 配置状态类，读取{@link FastCodingSettings}中的设置
 * <br/>
 * 注：状态类需要在plugin.xml中的extensions标签里声明
 *
 * @author JustryDeng
 * @since 2022/1/6 21:14:18
 */
@State(name = "fast-coding.idea.plugin", storages = {@Storage("setting.xml")})
public class AppSettingsState implements PersistentStateComponent<AppSettingsState> {
    
    /** [MockJson组件] mock json时是否包含注释 */
    public boolean MockJson$containComment;

    public static AppSettingsState getInstance(@NotNull Project project) {
        AppSettingsState appSettingsState = ServiceManager.getService(project, AppSettingsState.class);
       fillFieldValue(appSettingsState);
        return appSettingsState;
    }
    
    private static void fillFieldValue(AppSettingsState appSettingsState) {
        // 填充MockJson组件的相关字段值
        appSettingsState.MockJson$containComment = FastCodingConstant.TRUE.equals(PropertiesComponentUtil.getValue(FastCodingConstant.MockJson.containCommentKey));
    }


    @Nullable
    @Override
    public AppSettingsState getState() {
        return this;
    }

    @Override
    public void loadState(@NotNull AppSettingsState state) {
        XmlSerializerUtil.copyBean(state, this);
    }
}
